package com.jeeplus.modules.project.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.ProjectKnot;
import com.jeeplus.modules.project.entity.ProjectMidteamExam;
import com.jeeplus.modules.project.entity.vo.ProjectStatisticsVo;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.project.dao.ProjectKnotDao;

/**
 * 项目结项申请Service
 * @author jacky
 * @version 2018-01-28
 */
@Service
@Transactional(readOnly = true)
public class ProjectKnotService extends CrudService<ProjectKnotDao, ProjectKnot> {
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;

	public ProjectKnot get(String id) {
		return super.get(id);
	}
	
	public List<ProjectKnot> findList(ProjectKnot projectKnot) {
		return super.findList(projectKnot);
	}
	
	public Page<ProjectKnot> findPage(Page<ProjectKnot> page, ProjectKnot projectKnot) {
		return super.findPage(page, projectKnot);
	}
	
	@Transactional(readOnly = false)
	public void save(ProjectKnot projectKnot) {
		// 申请发起
		if (StringUtils.isBlank(projectKnot.getId())) {
			projectKnot.preInsert();
			dao.insert(projectKnot);
			//更新项目状态
			try {
				projectKnot.getBasePtInfo().setStatus(7); //表示申请结项状态
				baseProjectInfoService.updateStatus(projectKnot.getBasePtInfo());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 启动流程
			actTaskService.startProcess(ActUtils.PD_PROJECT_KNOT[0], ActUtils.PD_PROJECT_KNOT[1],
					projectKnot.getId(), "【项目结项】"+projectKnot.getBasePtInfo().getCourseName());
		}
		// 重新编辑申请
		else {
			projectKnot.preUpdate();
			dao.update(projectKnot);

			projectKnot.getAct().setComment(("yes".equals(projectKnot.getAct().getFlag()) ? "[重申] " : "[销毁] ")
					+ projectKnot.getAct().getComment());
			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass", "yes".equals(projectKnot.getAct().getFlag()) ? "1" : "0");
			actTaskService.complete(projectKnot.getAct().getTaskId(), projectKnot.getAct().getProcInsId(),
					projectKnot.getAct().getComment(), projectKnot.getBasePtInfo().getCourseName(), vars);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ProjectKnot projectKnot) {
		super.delete(projectKnot);
	}
	
	@Transactional(readOnly = false)
	public void approveSave(ProjectKnot projectKnot) {
		// 设置意见
		projectKnot.getAct().setComment(("yes".equals(projectKnot.getAct().getFlag())?"[同意] ":"[驳回] ")+projectKnot.getAct().getComment());
		projectKnot.preUpdate();
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = projectKnot.getAct().getTaskDefKey();
		// 审核环节
		if ("dept-review".equals(taskDefKey)){
			projectKnot.setDeptCheckAdvice(projectKnot.getAct().getComment());
		}
		else if ("dept-leader-review".equals(taskDefKey)){
			projectKnot.setDeptColledgeLeaderAdvice(projectKnot.getAct().getComment());
		}
		else if ("expert-review".equals(taskDefKey)){
			projectKnot.setExpertAdvice(projectKnot.getAct().getComment());
		}
		else if("academic-committee-review".equals(taskDefKey)){
			projectKnot.setAcademyAcademiaAdvice(projectKnot.getAct().getComment());
		}
		else if("finance-dept-review".equals(taskDefKey)){
			projectKnot.setFinanceAdvice(projectKnot.getAct().getComment());
		}
		else if ("big-boss-review".equals(taskDefKey)){
			projectKnot.setBossLeaderAdvice(projectKnot.getAct().getComment());
		}
		// 未知环节，直接返回
		else{
			return;
		}
		//更新数据
		dao.update(projectKnot);
		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		if ("academic-committee-review".equals(taskDefKey)){
			vars.put("aca_pass", "yes".equals(projectKnot.getAct().getFlag())? "1" : "0");
		}
		if ("finance-dept-review".equals(taskDefKey)){
			vars.put("fin_pass", "yes".equals(projectKnot.getAct().getFlag())? "1" : "0");
		}
		//最后一步如果同意，则更新项目信息状态
		if ("big-boss-review".equals(taskDefKey)){
			if("yes".equals(projectKnot.getAct().getFlag())){
				BaseProjectInfo baseInfo = projectKnot.getBasePtInfo();
				baseInfo.setStatus(8); //结项通过
				try {
					baseProjectInfoService.updateStatus(baseInfo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		vars.put("pass", "yes".equals(projectKnot.getAct().getFlag())? "1" : "0");
		actTaskService.complete(projectKnot.getAct().getTaskId(), projectKnot.getAct().getProcInsId(), projectKnot.getAct().getComment(), vars);
	}
	
	public List<ProjectStatisticsVo> projectKnotStatistics(Map<String,Object> map){
		return dao.projectKnotStatistics(map);
	}
	
}